package gemini.castor.ui.client.page.content.account.volumepoint.form;

import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.utils.GuiUtils;
import gemini.castor.ui.client.utils.HtmlUtils;

import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

public class VolumePointFormView extends Composite implements
		VolumePointFormPresenter.Display {

	@UiTemplate("VolumePointFormView.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, VolumePointFormView> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);
	private I18nConstants constants;

	public VolumePointFormView() {
		initWidget(binder.createAndBindUi(this));
		constants = GWT.create(I18nConstants.class);
	}

	@UiField
	HTML htmDate;

	@UiField
	HTML htmPersonallyPurchasedVolume;

	@UiField
	HTML htmGroupVolume;

	@UiField
	HTML htmTotalVolume;

	@UiField
	ListBox lbxYear;

	@UiField
	FlexTable ftbVolumePoint;

	@Override
	public Widget asWidget() {
		return this;
	}

	private void initVolumePointTable() {
		ColumnFormatter colFmt = ftbVolumePoint.getColumnFormatter();

		colFmt.setWidth(0, "15%");
		colFmt.setWidth(1, "25%");
		colFmt.setWidth(2, "25%");
		colFmt.setWidth(3, "35%");
	}

	@Override
	public int getYear() {
		return Integer.valueOf(lbxYear.getValue(lbxYear.getSelectedIndex()));
	}

	@Override
	public void loadData(List<VolumePointFormModel> models) {
		if (models != null && !models.isEmpty()) {
			initVolumePointTable();
			ftbVolumePoint.removeAllRows();
			initHeader(lbxYear.getValue(lbxYear.getSelectedIndex()));
			
			RowFormatter rowFmt = ftbVolumePoint.getRowFormatter();
			FlexCellFormatter cellFmt = ftbVolumePoint.getFlexCellFormatter();
			
			int i = 1;
			for (final VolumePointFormModel it : models) {
				if (i % 2 == 0) {
					rowFmt.setStyleName(i, "rowEven");
				}
				
				cellFmt.setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_RIGHT);
				cellFmt.setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_RIGHT);
				cellFmt.setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_RIGHT);
				
				ftbVolumePoint.setHTML(i, 0, String.valueOf(it.getMonth().getMonth()));
				ftbVolumePoint.setHTML(i, 1, String.valueOf(it.getPersonPoint()));
				ftbVolumePoint.setHTML(i, 2, String.valueOf(it.getGroupPoint()));
				ftbVolumePoint.setHTML(i, 3, String.valueOf(it.sumVolumePoint()));
				
				i++;
			}
		}
	}

	@Override
	public void showCurrentPoint(List<VolumePointFormModel> models) {
		Date date = new Date();
		for (VolumePointFormModel volumePointFormModel : models) {
			if (volumePointFormModel.getMonth().getMonth() == date.getMonth() + 1) {
				htmPersonallyPurchasedVolume.setText(String
						.valueOf(volumePointFormModel.getPersonPoint()));
				htmGroupVolume.setText(String.valueOf(volumePointFormModel
						.getGroupPoint()));
				htmTotalVolume.setText(String.valueOf(volumePointFormModel
						.sumVolumePoint()));
			}
		}
	}

	private void initHeader(String year) {
		ftbVolumePoint.setHTML(0, 0, HtmlUtils.bold(constants.htmYear() + " " + year));
		ftbVolumePoint.setHTML(0, 1, HtmlUtils.bold(constants.htmPersonalVolume()));
		ftbVolumePoint.setHTML(0, 2, HtmlUtils.bold(constants.htmGroupVolume()));
		ftbVolumePoint.setHTML(0, 3, HtmlUtils.bold(constants.htmTotalVolume()));
	}

	@Override
	public void initData(List<Integer> years) {
		if(years!=null){
			int currentYearIdx = 0;
			
			for (int j = 0; j < years.size(); j++) {
				if(years.get(j) == (new Date()).getYear() + 1900){
					currentYearIdx = j;
				}
				
				lbxYear.addItem(String.valueOf(years.get(j)));
			}
			
			lbxYear.setSelectedIndex(currentYearIdx);
			
			initVolumePointTable();
			initHeader(lbxYear.getValue(lbxYear.getSelectedIndex()));
	
			Date date = new Date();
			htmDate.setText(constants.htmVolumeTitle() + " "
					+ GuiUtils.format(date, GuiUtils.MM_YYYY));
		}
	}
	
	@Override
	public HasChangeHandlers getListBoxChangeHandler() {
		return lbxYear;
	}

	@Override
	public void loadData() {
	}
}
